package com.alex.jvm;

import java.util.concurrent.TimeUnit;

public class FinalizeGCTime {
    private static FinalizeGCTime gcTime = null;

    public static void main(String[] args) throws Throwable {
        test();
    }

    private static void test() throws Throwable {
        gcTime = new FinalizeGCTime();
        gcTime = null;
        System.gc();

        TimeUnit.SECONDS.sleep(1);
        if (gcTime != null) {
            System.out.println("Is Alive!");
        } else {
            System.out.println("Is Dead!");
        }

        TimeUnit.SECONDS.sleep(2);

        gcTime = null;
        System.gc();

        TimeUnit.SECONDS.sleep(1);
        if (gcTime != null) {
            System.out.println("Is Alive!");
        } else {
            System.out.println("Is Dead!");
        }

    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("Finalize is finished!");
        System.out.println(this);
        gcTime = this;
    }
}
